package org.fitsy.ui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.fitsy.FitsyConstants;
import org.jutils.IconConstants;
import org.jutils.ui.*;
import org.jutils.ui.model.IView;

/*******************************************************************************
 * 
 ******************************************************************************/
public class FitsyFrameView implements IView<JFrame>
{
    /**  */
    private final JFrame frame;
    /**  */
    private final FitsyView mainView;

    /***************************************************************************
     * 
     **************************************************************************/
    public FitsyFrameView()
    {
        this.frame = new JFrame();
        this.mainView = new FitsyView();

        frame.setTitle( FitsyConstants.APPLICATION_NAME );

        frame.setJMenuBar( createMenubar( frame ) );
        frame.setContentPane( createContentPane() );
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private Container createContentPane()
    {
        JPanel panel = new JPanel( new BorderLayout() );
        StatusBarPanel statusbar = new StatusBarPanel();

        panel.add( createToolbar(), BorderLayout.NORTH );
        panel.add( mainView.getView(), BorderLayout.CENTER );
        panel.add( statusbar.getView(), BorderLayout.SOUTH );

        return panel;
    }

    /***************************************************************************
     * @param frame
     * @return
     **************************************************************************/
    private JMenuBar createMenubar( JFrame frame )
    {
        JMenuBar menubar = new JGoodiesMenuBar();

        menubar.add( createFileMenu( frame ) );

        return menubar;
    }

    /***************************************************************************
     * @param frame
     * @return
     **************************************************************************/
    private JMenu createFileMenu( JFrame frame )
    {
        JMenu menu = new JMenu( "File" );
        JMenuItem item;

        item = new JMenuItem( "Open",
            IconConstants.loader.getIcon( IconConstants.OPEN_FOLDER_16 ) );
        item.addActionListener( new OpenListener() );
        menu.add( item );

        menu.addSeparator();

        item = new JMenuItem( "Exit",
            IconConstants.loader.getIcon( IconConstants.STOP_16 ) );
        item.addActionListener( new ExitListener( frame ) );
        menu.add( item );

        return menu;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private Component createToolbar()
    {
        JToolBar toolbar = new JGoodiesToolBar();
        JButton button;

        button = new JButton(
            IconConstants.loader.getIcon( IconConstants.OPEN_FOLDER_16 ) );
        button.setFocusable( false );
        button.addActionListener( new OpenListener() );
        toolbar.add( button );

        return toolbar;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public JFrame getView()
    {
        return frame;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private static final class OpenListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            // TODO Auto-generated method stub
        }
    }
}
